﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCoreLib;

namespace AvalonTycoonMansion.Shared
{
	[Script]
	public static class MyExtensions
	{
		public static string ToDollarString(this int value)
		{
			var w = new StringBuilder();

			if (value < 0)
			{
				w.Append("-");
				value = -value;
			}

			w.Append("$");

			var s = "" + value;
			var g = (s.Length - s.Length % 3) / 3 + 1;
			var d = 0;

			for (int i = 0; i < g; i++)
			{
				if (d > 0)
					w.Append(",");

				var j = s.Length - (g - i) * 3;
				var x = 3;

				if (j < 0)
				{
					x += j;
					j = 0;
					
				}

				if (x > 0)
				{
					d++;
					w.Append(s.Substring(j, x));
				}
			}

			return w.ToString();
		}
	}
}
